package player;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import player.miniplayer.SimpleMplayer;
import player.mplayer.MPlayerControl;
import player.mplayer.MediaInfo;
import player.ui.browse.ListPanel;
import player.ui.browse.FloatPanel;
import player.ui.browse.MyJSplitPane;
import player.ui.browse.TabPanel;
import player.ui.browse.TabPanel2;
import player.ui.controlbar.ControlBar;
import player.ui.controlbar.ControlBarFrame;
import player.ui.controlbar.PlayerProgressBar;
import player.ui.dialog.MyJTable;
import player.ui.dialog.OpenDialog;
import player.ui.playlist.PlayListManage;
import player.ui.playlist.PlayListModel;
import player.ui.tree.FileSystemTreeModel;
import player.ui.video.VideoCanvas;
import player.ui.video.VideoFrame;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;



public class PS {
	public static Config config;

	public static VideoFrame videoFrame;

	public static JFrame mframe;

	public static float videoRate=1.666f;

	public static FloatPanel FloatPanel;
	public static TabPanel BrowsePanel;
	public static OpenDialog OpenDialog;
	public static ListPanel FileBrowsePanel;
	public static PlayListManage PlayListManage;
	public static PlayListManage FileListManage;
	public static MyJSplitPane videoJSplitPane1;

	public static MyJTable leftButtomTable;

	public static MyJTable rightButtomTable;
	public static int playMode_suiji=5;
	public static VideoCanvas VideoCanvas;
	public static  PlayerProgressBar PlayerProgressBar;

	public static  TabPanel2 TabPanel2;
	
    public static String captureDir="capture\\";
    public static String captureTempDir="temp\\";
    
    public static PlayListManage nowPlayListManage;
    public static PlayListModel nowPlayListModel;
    public static MediaInfo nowPlayMedia=null;
    public static VideoCanvas videoCanvas;
    
   // public static JButton stopButton;
   // public static JButton playButton;
    
    public static ControlBar controlBar;
    
    public static void main(String[] args) {
		 start();
	}
    
	public static void start(){
		new Thread(){
			public void run(){
				Tools.init();
				loadConfig();
				MPlayerControl.init();
				FileSystemTreeModel.init();
				PlayListManage=new PlayListManage(true);
				PlayListManage.loadList(config.listList);
				FileListManage=new PlayListManage(false);
				if(!PlayListManage.containsPlayList(config.defaultPlayListName)){
					PlayListManage.addPlayerList(config.defaultPlayListName);
				}
				init();
				ThumbManage.init();
			}
		}.start();
		
	}
	
	public PS(){
		
	}
	
	

	static void setVideoRate(int f){
		videoRate=f;
		pack();
	}
	
	public static void pack(){
		if(videoFrame.isVisible()){
			VideoCanvas.pack();
		}
	}
	
	static void init(){
		try {
			SwingUtilities.invokeAndWait(new Runnable(){
				public void run() {
					initUi();
				}
			});
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		}
		try {
			SwingUtilities.invokeAndWait(new Runnable(){

				public void run() {

				}

			});
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(new Runnable(){

			public void run() {

			}

		});
	}

	static void loadConfig(){
		config=new Config();
		XStream xs = new XStream(new DomDriver());
		File cf=new File("configp.txt");
		if(cf.exists()){
			FileInputStream fis;
			try {
				fis = new FileInputStream(cf);
				config=(Config) xs.fromXML(new InputStreamReader(fis));
			} catch (FileNotFoundException e2) {
				e2.printStackTrace();
			}
		}
	}

	public static void saveConfig(){
		config.dialogSize=OpenDialog.getSize();
		config.l0Location=OpenDialog.firstJSP.getDividerLocation();
		config.l1Location=OpenDialog.leftJSP.getDividerLocation();
		config.l2Location=OpenDialog.rightJSP.getDividerLocation();
		config.l5Location=videoJSplitPane1.getDividerLocation();
		config.listList=PlayListManage.getList();
		XStream xs = new XStream(new DomDriver());
		FileOutputStream fs;
		try {
			fs = new FileOutputStream("configp.txt");
			xs.toXML(config, fs);
			fs.flush();
			fs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void initUi(){ 
		try {
			//LookAndFeel lf=new NimbusLookAndFeel();
			LookAndFeel lf=new  MyLAF();
			UIManager.setLookAndFeel(lf);
		}  catch (Exception e) {
			e.printStackTrace();
		}
		JDialog.setDefaultLookAndFeelDecorated(true);
		mframe=new JFrame();
		mframe.setBounds(-100, -100, 1, 1);
		mframe.setVisible(true);
		mframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				exit();
			}
		});  
		videoFrame=new VideoFrame(mframe);
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				OpenDialog=new OpenDialog();
				OpenDialog.setSize(config.dialogSize);
				OpenDialog.setLocationRelativeTo(null);
				//OpenDialog.setVisible(true);
			}
		});
		videoFrame.init();
		videoFrame.setBounds(config.playerBound);

		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		pack();

		videoFrame.setLocationRelativeTo(null);

		videoFrame.setVisible(true);
		//ControlFrame.setVisible(true);

	}

	public static long getWid(){
		return videoFrame.getWid();
	}

	public static void exit(){
		saveConfig();
		MPlayerControl.exit();
		SimpleMplayer.exit();
		System.exit(0);
	}

	public static void setUITime(float time){
		if(PlayerProgressBar!=null){
			PlayerProgressBar.setNowTime(time);	
		}
		
		
	}
	
	public static void setUILength(int length){
		PlayerProgressBar.setLength(length);
		
	}
	
	public static void checkFloatPanelLocation(){
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		Container cp=FloatPanel.getParent();
		
		Rectangle newRec=new Rectangle();
		newRec.width=cp.getWidth();
		newRec.height=cp.getHeight();
		
		Point pLT=new Point(0,0);
		SwingUtilities.convertPointFromScreen(pLT, cp);
		if(pLT.x>0){
			newRec.x=pLT.x;
			int nw=(cp.getWidth()-pLT.x);
			newRec.width=nw;
		}
		if(pLT.y>0){
			newRec.y=pLT.y;
			int nh=(cp.getHeight()-pLT.y);
			newRec.height=nh;
		}
		
		Point pRB=new Point(0,0);
		pRB.y=newRec.y+cp.getHeight();
		SwingUtilities.convertPointToScreen(pRB, cp);
		if(pRB.y>screenSize.height){
			Point p1=new Point(0,screenSize.height);
			SwingUtilities.convertPointFromScreen(p1, cp);
			newRec.height=p1.y-newRec.y;
		}
		
		Point pRT=new Point(0,0);
		pRT.x=newRec.x+cp.getWidth();
		SwingUtilities.convertPointToScreen(pRT, cp);
		if(pRT.x>screenSize.width){
			Point p1=new Point(screenSize.width,0);
			SwingUtilities.convertPointFromScreen(p1, cp);
			newRec.width=p1.x-newRec.x;
		}
		
		FloatPanel.setBounds(newRec);
		
	}



}
